﻿#include "unrealircd.h"
#include<time.h>
/*
此模块存放关于数据库操作的工具类函数


*/
int db_string_to_timestamp(D_TimeStamp *timestamp, char *stime);
int db_timestamp_to_string(const D_TimeStamp *timestamp, char *stime);
char * db_int_to_string(int num);
void D_getCurrectTime(D_TimeStamp *p);


//字符串转postgreSQL的时间戳
int db_string_to_timestamp(D_TimeStamp *timestamp,char *stime) {
	sscanf(stime, "%d-%d-%d %d:%d:%d",
		&(timestamp->year),
		&(timestamp->month),
		&(timestamp->day),
		&(timestamp->hour),
		&(timestamp->minute),
		&(timestamp->second));
	return 0;
}
//时间戳转化成字符串
int db_timestamp_to_string(const D_TimeStamp *timestamp, char *stime) {
	sprintf(stime,"%d-%d-%d %d:%d:%d", 
		(timestamp->year),
		(timestamp->month),
		(timestamp->day),
		(timestamp->hour),
		(timestamp->minute),
		(timestamp->second));
	return 0;
}
//int转化成字符串
char * db_int_to_string(int num) {
	char * temp = malloc(sizeof(char) * 30);
	sprintf(temp,"%d",num);
	return temp;
}
//返回当前的时间
void D_getCurrectTime(D_TimeStamp *p) {
	time_t timep;
	struct tm *pm;
	time(&timep);
	pm = localtime(&timep);
	p->year = pm->tm_year + 1900;
	p->month = pm->tm_mon+1;
	p->day = pm->tm_mday;
	p->hour = pm->tm_hour;
	p->minute = pm->tm_min;
	p->second = pm->tm_sec;

}

//返回根据time_t的时间戳
void D_getTimeByTime_t(D_TimeStamp *p,time_t *timep) {
	struct tm *pm;
	pm = localtime(timep);
	p->year = pm->tm_year + 1900;
	p->month = pm->tm_mon+1;
	p->day = pm->tm_mday;
	p->hour = pm->tm_hour;
	p->minute = pm->tm_min;
	p->second = pm->tm_sec;

}


/**
 * 查询IMConfigureSave链表中的configure_value对应的IMConfigureSave
 */
IMConfigureSave* D_FindImConfigureSaveNode(IMConfigureSave* head,char *configure_name){
	IMConfigureSave* temp=head->next;
	IMConfigureSave* pre=head;
	while(temp){
		if(!strcmp(temp->configure_name,configure_name)){
			if(temp->lastQueryTime<time(0)-5*60){
				break;
			}
			return temp;
		}
		pre=temp;
		temp=temp->next;
	}
	char *configure_value=queryImConfigureValue(configure_name);
	if(configure_value){
		if(temp){
			safe_free(temp->configure_value);
			temp->configure_value=configure_value;
			temp->lastQueryTime=time(0);
			return temp;
		}
		else{
			IMConfigureSave *save=safe_alloc(sizeof(IMConfigureSave));
			save->configure_name=NULL;
			safe_strdup(save->configure_name,configure_name);
			save->configure_value=configure_value;
			save->next=NULL;
			save->lastQueryTime=time(0);
			pre->next=save;
			return save;
		}
	}
	return NULL;
}


/**
 * 将PGresult中int值返回回来
 */
int *PQresult_row_col_To_int(PGresult *result,int row,int col){
	if (!PQgetisnull(result, row, col)) {
		char *value = PQgetvalue(result, row, col);
		int *temp=malloc(sizeof(int));
		*(temp)=atoi(value);
		return temp;
	}
	else{
		return NULL;
	}
}

/**
 * 将PGresult中char值返回回来
 */
char *PQresult_row_col_To_char(PGresult *result,int row,int col){
	if (!PQgetisnull(result, row, col)) {
		char *value = PQgetvalue(result, row, col);
		char *temp=NULL;
		safe_strdup(temp,value);
		return temp;
	}
	else{
		return NULL;
	}
}

/**
 * 将PGresult中char值返回回来
 */
D_TimeStamp *PQresult_row_col_To_D_TimeStamp(PGresult *result,int row,int col){
	if (!PQgetisnull(result, row, col)) {
		char *value = PQgetvalue(result, row, col);
		D_TimeStamp *timestamp=safe_alloc(sizeof(D_TimeStamp));
		db_string_to_timestamp(timestamp,value);
		return timestamp;
	}
	else{
		return NULL;
	}
}

//查询是否验证码有效，有效返回1，无效返回0
int D_SMSVerificationCode(char * contactvalue, char *code,SMS_MODE channal,SMS_STATUS status){
	int sms_data_len =0;
	D_im_sms *sms_data=queryImSMSByValueAndType(contactvalue,status,channal,5,&sms_data_len);
	if(sms_data_len <= 0){
		return 0;
	}
	int flag = 0;
	for(int i= 0;i < sms_data_len; ++i){
		if(!strcmp(sms_data[i].code,code)){
			*(sms_data[i].used) = 1;
			int sms_updata = 0;
			char s_sms_updata[255] = "";
			D_im_sms *temp = updataImSMS(sms_data,&sms_updata,s_sms_updata);
			db_D_ImSMSFree(temp,sms_updata);
			flag = 1;
			break;
		}
	}
	for(int i=0; i< sms_data_len; ++i){
		db_D_ImSMSFree(sms_data+i,sms_data_len);
	}
	return flag;
}